﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using DAL;

namespace Web
{
    public partial class PageTeacher : Page
    {
        protected Teacher _teacher;
        private TeacherBll _teacherBll;

        protected int SameNameTeachersNum;

        protected void Page_Load(object sender, EventArgs e)
        {
            var id = Request["id"];
            if (id == null)
            {
                Response.Redirect("home.aspx");
                return;
            }
            _teacherBll = new TeacherBll(int.Parse(id));
            _teacher = _teacherBll.GetTeacher();
            if (_teacher == null)
            {
                Response.Redirect("home.aspx");
                return;
            }
            Initialize();
        }

        private void Initialize()
        {
            SameNameTeachers_Load(_teacherBll.GetSameNameTeachers());            
            if (SameNameTeachersNum == 0)
            {
                samenameteachers_div.Visible = false;
                upsideline_div.Width = 742;
            }
            else
                upsideline_div.Width = 542;
            TaughtCourse_Load(_teacherBll.GetTaughtCourses());
        }

        // 加载同名教师
        private void SameNameTeachers_Load(List<Teacher> data)
        {
            samename_repeater.DataSource = data;
            samename_repeater.DataBind();
            SameNameTeachersNum = data.Count;
        }

        // 加载所授课程
        private void TaughtCourse_Load(List<Course> data)
        {
            taughtcourses_repeater.DataSource = data;
            taughtcourses_repeater.DataBind();
        }

        // 点击搜索按钮
        protected void HeaderSearchBox_Click(object sender, EventArgs e)
        {
            Response.Redirect("search.aspx?type=10&value=" + search_input.Text);
        }

        // 课程数据绑定
        protected void TaughtCourses_Bound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem)
                return;
            ((Literal)e.Item.FindControl("course_item")).Text
                = e.Item.ItemIndex % 3 == 2 ? "<li style=\"margin-right:0;\">" : "<li>";
        }
    }
}